package com.gzsxy.excel.log;


import com.gzsxy.excel.log.aspect.SysLogAspect;
import com.gzsxy.excel.log.event.SysLogListener;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author chenguoyi
 * @Description:
 * @date 2022/1/4 17:55
 */
@EnableAsync //开启异步方法
@RequiredArgsConstructor
@ConditionalOnWebApplication //当前项目是Web项目的条件下。
//ture -Full模式下通过方法调用指向的仍旧是原来的Bean
@Configuration(proxyBeanMethods = false) // 是lite模式，直接返回新实例对象;提高Spring启动速度
public class LogAutoConfiguration {

    @Bean
    public SysLogListener sysLogListener() {
        return new SysLogListener();
    }

    @Bean
    public SysLogAspect sysLogAspect() {
        return new SysLogAspect();
    }


    @Bean(name = "sdqdThreadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心池大小
        executor.setCorePoolSize(6);
        //队列程度
        executor.setQueueCapacity(1000);
        //最大线程数
        executor.setMaxPoolSize(30);
        //线程空闲时间
        executor.setKeepAliveSeconds(1000);
        //线程前缀名称
        executor.setThreadNamePrefix("sdqd-thread-executor-");
        //配置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //初始化
        executor.initialize();
        return executor;
    }

}
